package com.nyist.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.nyist.entity.Manage;
import com.nyist.entity.Role;
import com.nyist.service.ManageService;
import com.nyist.service.RelationService;
/**
 *  登录的时候进行判断 
 *  1，验证码，2，密码3，是否已经邮箱验证过了，要是验证过了就放行，没有验证不能进入
 * @author 18077
 *
 */
@Controller
@RequestMapping("manage")
public class ManageLoginController {
	@Autowired
	@Qualifier("manageService")
	private ManageService manageService;
	
	@Autowired
	@Qualifier("relationService")
	private RelationService relationService;
	
	@RequestMapping(value="checkLogin")
	public void checkLogin(String username,String password,String validatecode,HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		
		/*从session 取出登录时候系统产生的验证码的值*/
		String code = (String)request.getSession().getAttribute("validationCode");
		/*判断输入的验证码是否成功*/
		if(code.equals(validatecode)){
			/*根据用户名从数据库中取出整个对象*/
			Manage manage = manageService.queryForRegist(username);
			//数据库存进的数据 密码和验证码
			String pwd = manage.getPassword();
			String vod = manage.getValidatecode();
			//将用户输入的密码和之前存进的验证码进行加密，然后与数据库中的密码进行判断是否正确
			String newpwd = DigestUtils.md5Hex(password+vod);
			if(!newpwd.equals(pwd)){
				manage=null;
			}
			//System.out.println(manage);
			PrintWriter out = response.getWriter();
			if(manage==null){
				 out.print("<h3 align='center'>用户密或密码不正确</h3>");
			}
			
			
			// 原本验证manage.getState()==0   由于是将验证取消
			
			
			else if(manage.getState()==1) {/* 判断是否已经注册，从数据库中得到的对象，然后取出邮箱验证码状态 若为0那么就是没有验证过，若是为1那么就是已经验证过了*/
				/*还没有验证过，开始提供验证信息，组装邮件信息*/
				
				//获取IP
		        InetAddress addr = InetAddress.getLocalHost();
				String ip=addr.getHostAddress().toString();
		        String url = "http://";
		        url+=ip+":";
		        url+=request.getLocalPort();
		        url+=request.getContextPath();
		        /*设置回馈，进行响应，进入响应的Controller 中然后将邮箱验证状态改一下*/
		        url+="/manage/activation.do?id=";
				url+=manage.getId();
				url+="&name=";
				url+=manage.getUsername();
				//System.out.println("url-----------"+url);
				out.print("<h3 align='center'>用户还没有被激活</h3>");
				/*使用Session 将url 和 mail 传递到重新发送邮件验证信息的页面*/
				HttpSession session = request.getSession();
				session.setAttribute("url", url);
				session.setAttribute("mail", manage.getMail());
				out.print("<a href='/RegistLogin/reSendMail.jsp'> 重新发送激活邮件</a>");
				out.close();
			}
			else{
				String manageid = manage.getId();
				int role = relationService.queryRoleByManageid(manageid);
				
				String description = relationService.queryDiscriptionByManageid(role);
				//System.out.println(description);
				HttpSession session = request.getSession();
				if(role == 1){
					response.sendRedirect("/RegistLogin/super/manageList.do");
				}
				else if(role == 2){
					session.setAttribute("role", "普通管理员");
					response.sendRedirect("/RegistLogin/success1.jsp");
				}
				else{
					session.setAttribute("role", "普通用户");
					response.sendRedirect("/RegistLogin/success2.jsp");
				}
			}
		}else{
			response.sendRedirect("/RegistLogin/login.jsp");
		}
		
	}
}
